package com.lv.fast.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lv.fast.DTO.SysRoleDTO;
import com.lv.fast.DTO.SysRolePageQuery;
import com.lv.fast.annotation.SysLog;
import com.lv.fast.enums.OperateTypeEnum;
import com.lv.fast.model.RestResult;
import com.lv.fast.service.SysRoleService;
import com.lv.fast.valid.group.AddStrategy;
import com.lv.fast.valid.group.UpdateStrategy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Author jie.lv
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/role")
@Api(tags = "后台管理---角色模块")
@ApiSupport(author = "lv")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @GetMapping("/list")
    @ApiOperation(value = "获取角色列表")
    @ApiOperationSupport(order = 1)
    @RequiresPermissions("sys:role:list")
    public RestResult list(@Validated SysRolePageQuery pageQuery){
        IPage<SysRoleDTO> page = sysRoleService.list(pageQuery);
        return RestResult.success(page);
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询角色详情")
    @ApiOperationSupport(order = 2)
    @RequiresPermissions("sys:role:info")
    public RestResult get(@PathVariable String id){
        SysRoleDTO sysRoleDTO = sysRoleService.get(id);
        return RestResult.success(sysRoleDTO);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    @ApiOperationSupport(order = 3)
    @RequiresPermissions("sys:role:insert")
    @SysLog(operateType = OperateTypeEnum.INSERT, value = "新增角色")
    public RestResult add(@RequestBody @Validated(AddStrategy.class) SysRoleDTO roleDTO){
        return RestResult.build(sysRoleService.add(roleDTO));
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改角色")
    @ApiOperationSupport(order = 4)
    @RequiresPermissions("sys:role:update")
    @SysLog(operateType = OperateTypeEnum.UPDATE, value = "修改角色")
    public RestResult update(@RequestBody @Validated(UpdateStrategy.class) SysRoleDTO roleDTO){
        return RestResult.build(sysRoleService.update(roleDTO));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除角色")
    @ApiOperationSupport(order = 5)
    @RequiresPermissions("sys:role:delete")
    @SysLog(operateType = OperateTypeEnum.DELETE, value = "删除角色")
    public RestResult delete(@PathVariable String id){
        return RestResult.build(sysRoleService.delete(id));
    }
}
